package com.hxm.meizi.copytoserver;

import android.view.View;

import com.blankj.utilcode.utils.LogUtils;
import com.google.gson.Gson;
import com.hxm.meizi.R;
import com.hxm.meizi.app.Constants;
import com.hxm.meizi.http.JsoupDoc;
import com.hxm.meizi.ui.activity.BaseActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by hxm on 2017/2/25.
 * upload
 */

public class UploadActivity extends BaseActivity {
    private List<DetailInfo> detailInfos = new ArrayList<>();
    private List<DetailInfo> newDetail = new ArrayList<>();
    private List<ImageList> imageLists;
    private boolean isOk = false;
    private List<String> imageId = new ArrayList<>();

    @Override
    protected int setLayoutId() {
        return R.layout.activity_upload;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    public void upload(View view) {
        JsoupDoc.parseTypeUpload(Constants.BASE_URL, new ImageInfoListener() {
            @Override
            public void onSuccess(final ArrayList<DetailInfo> imageDInfo) {
                Observable.from(imageDInfo)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .map(new Func1<DetailInfo, NewDetailInfo>() {
                            @Override
                            public NewDetailInfo call(final DetailInfo detailInfo) {
                                final NewDetailInfo ddd = new NewDetailInfo();
                                ddd.setUrl(detailInfo.getUrl());
                                ddd.setPublishTime(detailInfo.getPublishTime());
                                ddd.setReadCount(detailInfo.getReadCount());
                                ddd.setDescribe(detailInfo.getDescribe());
                                ddd.setClassId(detailInfo.getClassId());
                                JsoupDoc.getImageDetailsUpload(detailInfo.getImageId(), new ImageLoadingListener() {
                                    @Override
                                    public void onSuccess(ImageList objects) {
                                        ddd.setPictureCount(objects.getImageNums());
                                        ddd.setPictures(objects.getUrls());
                                    }
                                });
                                return ddd;
                            }
                        })
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Action1<NewDetailInfo>() {
                            @Override
                            public void call(NewDetailInfo detailInfo) {
                                int classId = detailInfo.getClassId();
                                String describe = detailInfo.getDescribe();
                                int count = detailInfo.getPictureCount();
                                List<String> pictures = detailInfo.getPictures();
                                String time = detailInfo.getPublishTime();
                                int viewTimes = detailInfo.getReadCount();
                                String url = detailInfo.getUrl();
                                Map<String, Object> map = new HashMap<String, Object>();
                                map.put("classId", classId);
                                map.put("describe", describe);
                                map.put("publishTime", time);
                                map.put("readCount", viewTimes);
                                map.put("pictureCount", count);
                                map.put("url", url);
                                map.put("special", 0);
                                map.put("pictures", pictures);
                                Gson gson = new Gson();
                                String json = gson.toJson(map);
                                LogUtils.i(json);
//                                HttpRequest.getInstance()
//                                        .uploadInfo(json)
//                                        .subscribeOn(Schedulers.io())
//                                        .observeOn(AndroidSchedulers.mainThread())
//                                        .subscribe(new Action1<ResponseBody>() {
//                                            @Override
//                                            public void call(ResponseBody responseBody) {
//                                                LogUtils.i("upload", responseBody.toString());
//                                            }
//                                        }, new Action1<Throwable>() {
//                                            @Override
//                                            public void call(Throwable throwable) {
//                                                LogUtils.i("uploaderror", throwable.toString());
//                                            }
//                                        });
                            }
                        }, new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                LogUtils.i("uploaderror", throwable.toString());
                            }
                        });
            }
        });
    }

    public void upload2(View view) {

    }


}
